// packageI/public_welfare_fund/donations/donations.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        available_amount: "",
        available_amount_num: 0,
        amount: "",
        remarks: "",
        language: {},
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let language = wx.getStorageSync("langIndex");
        this.setData({ language: language.en });

        this.initData();
        this.getExpendData();
    },

    getExpendData() {
        let urlStr = app.getNetAddresss("plugin.public-fund.frontend.controllers.fund.expendIndex");
        wx.showLoading({
          title: 'loading',
        })
        app._getNetWork({
            url: urlStr,
            data: {},
            success: (res) => {
                if (res.data.result !== 1) {
                    wx.showToast({ title: res.data.msg});
                    return;
                }
                let { amount, is_expend_member } = res.data.data;
                this.setData({
                    available_amount: amount,
                    available_amount_num: parseFloat(amount.replace(",", ""))
                })

                if (!is_expend_member) {
                    wx.navigateBack({
                      delta: 1,
                    })
                }
            },
            complete: () => {
                wx.hideLoading();
            }
        })
    },

    debounceMethods() {
        let that = this;
        if (this.debounceTimer) clearTimeout(this.debounceTimer);
        let callNow = !this.debounceTimer; //是否立即执行
        this.debounceTimer = setTimeout(() => {
          this.debounceTimer = null;
        }, 800);
        if (callNow) that.submitRequest.apply(that);
      },

    submitRequest() {
        if (!this.data.amount) {
            wx.showToast({
                title: '请输入支出金额',
            })
            return;
        }
        if (!this.data.remarks) {
            wx.showToast({
                title: '请输入备注',
            })
            return;
        }
        if (this.data.amount > this.data.available_amount_num) {
            wx.showToast({
                title: '支出金额不能大于可支出金额',
            })
            return;
        }
        let urlStr = app.getNetAddresss("plugin.public-fund.frontend.controllers.fund.expend");
        wx.showLoading({
          title: 'loading',
        })
        app._getNetWork({
            url: urlStr,
            data: {
                amount: this.data.amount,
                remark: this.data.remarks
            },
            success: (res) => {
                if (res.data.result !== 1) {
                    wx.showToast({ title: res.data.msg});
                    return;
                }
                wx.showToast({ 
                    title: "申请成功",
                });
                this.timer = setTimeout(() => {
                    wx.redirectTo({ url: '../../public_welfare_fund/publicWelfare/publicWelfare',})
                }, 500)
                
            },
            complete: () => {
                wx.hideLoading();
            }
        })
      },

    changeAmount (event) {
        this.setData({
            amount: event.detail,
        })
    },

    changeRemarks (event) {
        this.setData({
            remarks: event.detail
        })
    },

    clickAll () {
        this.setData({
            amount: this.data.available_amount
        })
    },

    initData() {
        this.setData({
            available_amount: 0,
            amount: "",
            remarks: ""
        })
      },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    onHide () {
        clearTimeout(this.timer)
    },
})